/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package ar.edu.utn.frsf.cidisi.ginet.abstractginet.impl;

import ar.edu.utn.frsf.cidisi.ginet.Page;

import ar.edu.utn.frsf.cidisi.ginet.abstractginet.AbstractginetFactory;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.AbstractginetPackage;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Transition;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.pnml.tools.epnk.pnmlcoremodel.AnnotationGraphics;
import org.pnml.tools.epnk.pnmlcoremodel.Coordinate;
import org.pnml.tools.epnk.pnmlcoremodel.Name;
import org.pnml.tools.epnk.pnmlcoremodel.NodeGraphics;
import org.pnml.tools.epnk.pnmlcoremodel.PlaceNode;
import org.pnml.tools.epnk.pnmlcoremodel.PnmlcoremodelFactory;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Transition</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link ar.edu.utn.frsf.cidisi.ginet.abstractginet.impl.TransitionImpl#getParallelCardinality <em>Parallel Cardinality</em>}</li>
 *   <li>{@link ar.edu.utn.frsf.cidisi.ginet.abstractginet.impl.TransitionImpl#getRefPage <em>Ref Page</em>}</li>
 *   <li>{@link ar.edu.utn.frsf.cidisi.ginet.abstractginet.impl.TransitionImpl#getSequentialCardinality <em>Sequential Cardinality</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class TransitionImpl extends ar.edu.utn.frsf.cidisi.ginet.impl.TransitionImpl implements Transition {
	/**
	 * The default value of the '{@link #getParallelCardinality() <em>Parallel Cardinality</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getParallelCardinality()
	 * @generated
	 * @ordered
	 */
	protected static final String PARALLEL_CARDINALITY_EDEFAULT = "1";

	/**
	 * The cached value of the '{@link #getParallelCardinality() <em>Parallel Cardinality</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getParallelCardinality()
	 * @generated
	 * @ordered
	 */
	protected String parallelCardinality = PARALLEL_CARDINALITY_EDEFAULT;

	/**
	 * The cached value of the '{@link #getRefPage() <em>Ref Page</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getRefPage()
	 * @generated
	 * @ordered
	 */
	protected Page refPage;

	/**
	 * The default value of the '{@link #getSequentialCardinality() <em>Sequential Cardinality</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getSequentialCardinality()
	 * @generated
	 * @ordered
	 */
	protected static final String SEQUENTIAL_CARDINALITY_EDEFAULT = "1";

	/**
	 * The cached value of the '{@link #getSequentialCardinality() <em>Sequential Cardinality</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getSequentialCardinality()
	 * @generated
	 * @ordered
	 */
	protected String sequentialCardinality = SEQUENTIAL_CARDINALITY_EDEFAULT;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected TransitionImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return AbstractginetPackage.Literals.TRANSITION;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getParallelCardinality() {
		return parallelCardinality;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setParallelCardinality(String newParallelCardinality) {
		String oldParallelCardinality = parallelCardinality;
		parallelCardinality = newParallelCardinality;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, AbstractginetPackage.TRANSITION__PARALLEL_CARDINALITY, oldParallelCardinality, parallelCardinality));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Page getRefPage() {
		if (refPage != null && refPage.eIsProxy()) {
			InternalEObject oldRefPage = (InternalEObject)refPage;
			refPage = (Page)eResolveProxy(oldRefPage);
			if (refPage != oldRefPage) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, AbstractginetPackage.TRANSITION__REF_PAGE, oldRefPage, refPage));
			}
		}
		return refPage;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Page basicGetRefPage() {
		return refPage;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setRefPage(Page newRefPage) {
		Page oldRefPage = refPage;
		refPage = newRefPage;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, AbstractginetPackage.TRANSITION__REF_PAGE, oldRefPage, refPage));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getSequentialCardinality() {
		return sequentialCardinality;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setSequentialCardinality(String newSequentialCardinality) {
		String oldSequentialCardinality = sequentialCardinality;
		sequentialCardinality = newSequentialCardinality;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, AbstractginetPackage.TRANSITION__SEQUENTIAL_CARDINALITY, oldSequentialCardinality, sequentialCardinality));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case AbstractginetPackage.TRANSITION__PARALLEL_CARDINALITY:
				return getParallelCardinality();
			case AbstractginetPackage.TRANSITION__REF_PAGE:
				if (resolve) return getRefPage();
				return basicGetRefPage();
			case AbstractginetPackage.TRANSITION__SEQUENTIAL_CARDINALITY:
				return getSequentialCardinality();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case AbstractginetPackage.TRANSITION__PARALLEL_CARDINALITY:
				setParallelCardinality((String)newValue);
				return;
			case AbstractginetPackage.TRANSITION__REF_PAGE:
				setRefPage((Page)newValue);
				return;
			case AbstractginetPackage.TRANSITION__SEQUENTIAL_CARDINALITY:
				setSequentialCardinality((String)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case AbstractginetPackage.TRANSITION__PARALLEL_CARDINALITY:
				setParallelCardinality(PARALLEL_CARDINALITY_EDEFAULT);
				return;
			case AbstractginetPackage.TRANSITION__REF_PAGE:
				setRefPage((Page)null);
				return;
			case AbstractginetPackage.TRANSITION__SEQUENTIAL_CARDINALITY:
				setSequentialCardinality(SEQUENTIAL_CARDINALITY_EDEFAULT);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case AbstractginetPackage.TRANSITION__PARALLEL_CARDINALITY:
				return PARALLEL_CARDINALITY_EDEFAULT == null ? parallelCardinality != null : !PARALLEL_CARDINALITY_EDEFAULT.equals(parallelCardinality);
			case AbstractginetPackage.TRANSITION__REF_PAGE:
				return refPage != null;
			case AbstractginetPackage.TRANSITION__SEQUENTIAL_CARDINALITY:
				return SEQUENTIAL_CARDINALITY_EDEFAULT == null ? sequentialCardinality != null : !SEQUENTIAL_CARDINALITY_EDEFAULT.equals(sequentialCardinality);
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (parallelCardinality: ");
		result.append(parallelCardinality);
		result.append(", sequentialCardinality: ");
		result.append(sequentialCardinality);
		result.append(')');
		return result.toString();
	}
	

	@Override
	public boolean isConnectedTo(PlaceNode node) {
		
		EList<org.pnml.tools.epnk.pnmlcoremodel.Arc> arcs = new BasicEList<org.pnml.tools.epnk.pnmlcoremodel.Arc>();
		arcs.addAll(this.getIn());
		arcs.addAll(this.getOut());
		
		for (org.pnml.tools.epnk.pnmlcoremodel.Arc arc:arcs){
			
			if (arc.getSource().getId().equals(node.getId()) || arc.getTarget().getId().equals(node.getId()))
				return true;
			
		}
		
		return false;
	}
	
	@Override
	public Transition clone(){

		float x = ((NodeGraphics)this.getGraphics()).getPosition().getX();
		float y = ((NodeGraphics)this.getGraphics()).getPosition().getY();
		
		Transition newTransition = AbstractginetFactory.eINSTANCE.createTransition();
		newTransition.setId(id);	// Set the transition's id
		
		NodeGraphics nodeGraphics = PnmlcoremodelFactory.eINSTANCE.createNodeGraphics();

		// Creates a coordinate position for the transition
		Coordinate position = PnmlcoremodelFactory.eINSTANCE.createCoordinate();
		position.setX(x);
		position.setY(y);
		nodeGraphics.setPosition(position);
		
		// Set the size of the transition
		Coordinate size = PnmlcoremodelFactory.eINSTANCE.createCoordinate();
		size.setX(40);
		size.setY(40);
		nodeGraphics.setDimension(size);
		
		newTransition.setGraphics(nodeGraphics);
		
		// Creates a label for the transition
		Name nameLabel = PnmlcoremodelFactory.eINSTANCE.createName();
		nameLabel.setText(this.getName().getText());
		AnnotationGraphics labelPos = PnmlcoremodelFactory.eINSTANCE.createAnnotationGraphics();
		Coordinate offset = PnmlcoremodelFactory.eINSTANCE.createCoordinate();
		offset.setX(0);
		offset.setY(30);
		labelPos.setOffset(offset);
		nameLabel.setGraphics(labelPos);
		newTransition.setName(nameLabel);
		
		
		return newTransition;
	}
	

} //TransitionImpl
